<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>prompt.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_prompt.js.html">prompt.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2006 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview DHTML prompt to replace javascript&#39;s prompt().
<a name="line17"></a> *
<a name="line18"></a> * @see ../demos/prompt.html
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.ui.Prompt&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.Timer&#39;);
<a name="line25"></a>goog.require(&#39;goog.dom&#39;);
<a name="line26"></a>goog.require(&#39;goog.events&#39;);
<a name="line27"></a>goog.require(&#39;goog.ui.Component.Error&#39;);
<a name="line28"></a>goog.require(&#39;goog.ui.Dialog&#39;);
<a name="line29"></a>goog.require(&#39;goog.ui.Dialog.ButtonSet&#39;);
<a name="line30"></a>goog.require(&#39;goog.ui.Dialog.DefaultButtonKeys&#39;);
<a name="line31"></a>goog.require(&#39;goog.ui.Dialog.EventType&#39;);
<a name="line32"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line33"></a>
<a name="line34"></a>/**
<a name="line35"></a> * Creates an object that represents a prompt (used in place of javascript&#39;s
<a name="line36"></a> * prompt). The html structure of the prompt is the same as the layout for
<a name="line37"></a> * dialog.js except for the addition of a text box which is placed inside the
<a name="line38"></a> * &quot;Content area&quot; and has the default class-name &#39;modal-dialog-userInput&#39;
<a name="line39"></a> *
<a name="line40"></a> * @param {string} promptTitle The title of the prompt.
<a name="line41"></a> * @param {string} promptText The text of the prompt.
<a name="line42"></a> * @param {Function} callback The function to call when the user selects Ok or
<a name="line43"></a> *     Cancel. The function should expect a single argument which represents
<a name="line44"></a> *     what the user entered into the prompt. If the user presses cancel, the
<a name="line45"></a> *     value of the argument will be null.
<a name="line46"></a> * @param {string} opt_defaultValue Optional default value that should be in
<a name="line47"></a> *     the text box when the prompt appears.
<a name="line48"></a> * @param {string} opt_class Optional prefix for the classes.
<a name="line49"></a> * @param {boolean} opt_useIframeForIE For IE, workaround windowed controls
<a name="line50"></a> *     z-index issue by using a an iframe instead of a div for bg element.
<a name="line51"></a> * @param {goog.dom.DomHelper} opt_domHelper Optional DOM helper; see {@link
<a name="line52"></a> *    goog.ui.Component} for semantics.
<a name="line53"></a> * @constructor
<a name="line54"></a> * @extends {goog.ui.Dialog}
<a name="line55"></a> */
<a name="line56"></a>goog.ui.Prompt = function(promptTitle, promptText, callback, opt_defaultValue,
<a name="line57"></a>    opt_class, opt_useIframeForIE, opt_domHelper) {
<a name="line58"></a>  goog.ui.Dialog.call(this, opt_class, opt_useIframeForIE, opt_domHelper);
<a name="line59"></a>
<a name="line60"></a>  this.setTitle(promptTitle);
<a name="line61"></a>  this.setContent(promptText + &#39;&lt;br&gt;&lt;br&gt;&#39;);
<a name="line62"></a>  this.callback_ = callback;
<a name="line63"></a>  this.defaultValue_ = goog.isDef(opt_defaultValue) ? opt_defaultValue : &#39;&#39;;
<a name="line64"></a>
<a name="line65"></a>  goog.events.listen(
<a name="line66"></a>      this, goog.ui.Dialog.EventType.SELECT, this.onPromptExit_);
<a name="line67"></a>
<a name="line68"></a>  /** @desc label for a dialog button. */
<a name="line69"></a>  var MSG_PROMPT_OK = goog.getMsg(&#39;OK&#39;);
<a name="line70"></a>  /** @desc label for a dialog button. */
<a name="line71"></a>  var MSG_PROMPT_CANCEL = goog.getMsg(&#39;Cancel&#39;);
<a name="line72"></a>  var buttonSet = new goog.ui.Dialog.ButtonSet(opt_domHelper);
<a name="line73"></a>  buttonSet.set(goog.ui.Dialog.DefaultButtonKeys.OK, MSG_PROMPT_OK, true);
<a name="line74"></a>  buttonSet.set(goog.ui.Dialog.DefaultButtonKeys.CANCEL,
<a name="line75"></a>      MSG_PROMPT_CANCEL, false, true);
<a name="line76"></a>  this.setButtonSet(buttonSet);
<a name="line77"></a>};
<a name="line78"></a>goog.inherits(goog.ui.Prompt, goog.ui.Dialog);
<a name="line79"></a>
<a name="line80"></a>
<a name="line81"></a>/**
<a name="line82"></a> * Callback function which is invoked with the response to the prompt
<a name="line83"></a> * @type {Function}
<a name="line84"></a> * @private
<a name="line85"></a> */
<a name="line86"></a>goog.ui.Prompt.prototype.callback_ = goog.nullFunction;
<a name="line87"></a>
<a name="line88"></a>
<a name="line89"></a>/**
<a name="line90"></a> * Default value to display in prompt window
<a name="line91"></a> * @type {string}
<a name="line92"></a> * @private
<a name="line93"></a> */
<a name="line94"></a>goog.ui.Prompt.prototype.defaultValue_ = &#39;&#39;;
<a name="line95"></a>
<a name="line96"></a>
<a name="line97"></a>/**
<a name="line98"></a> * Element in which user enters response (HTML &lt;input&gt; text box)
<a name="line99"></a> * @type {HTMLInputElement?}
<a name="line100"></a> * @private
<a name="line101"></a> */
<a name="line102"></a>goog.ui.Prompt.prototype.userInputEl_ = null;
<a name="line103"></a>
<a name="line104"></a>
<a name="line105"></a>/**
<a name="line106"></a> * Tracks whether the prompt is in the process of closing to prevent multiple
<a name="line107"></a> * calls to the callback when the user presses enter.
<a name="line108"></a> * @type {boolean}
<a name="line109"></a> * @private
<a name="line110"></a> */
<a name="line111"></a>goog.ui.Prompt.prototype.isClosing_ = false;
<a name="line112"></a>
<a name="line113"></a>
<a name="line114"></a>/**
<a name="line115"></a> * Number of rows in the user input element.
<a name="line116"></a> * The default is 1 which means use an &lt;input&gt; element.
<a name="line117"></a> * @type {number}
<a name="line118"></a> * @private
<a name="line119"></a> */
<a name="line120"></a>goog.ui.Prompt.prototype.rows_ = 1;
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * Number of cols in the user input element.
<a name="line125"></a> * The default is 0 which means use browser default.
<a name="line126"></a> * @type {number}
<a name="line127"></a> * @private
<a name="line128"></a> */
<a name="line129"></a>goog.ui.Prompt.prototype.cols_ = 0;
<a name="line130"></a>
<a name="line131"></a>
<a name="line132"></a>/**
<a name="line133"></a> * Set the number of rows in the user input element.
<a name="line134"></a> * A values of 1 means use an &lt;input&gt; element.  If the prompt is already
<a name="line135"></a> * rendered then you cannot change from &lt;input&gt; to &lt;textarea&gt; or vice versa.
<a name="line136"></a> * @param {number} rows Number of rows for user input element.
<a name="line137"></a> * @throws {goog.ui.Component.Error.ALREADY_RENDERED} If the component is
<a name="line138"></a> *    already rendered and an attempt to change between &lt;input&gt; and &lt;textarea&gt;
<a name="line139"></a> *    is made.
<a name="line140"></a> */
<a name="line141"></a>goog.ui.Prompt.prototype.setRows = function(rows) {
<a name="line142"></a>  if (this.isInDocument()) {
<a name="line143"></a>    if (this.userInputEl_.tagName.toLowerCase() == &#39;input&#39;) {
<a name="line144"></a>      if (rows &gt; 1) {
<a name="line145"></a>        throw Error(goog.ui.Component.Error.ALREADY_RENDERED);
<a name="line146"></a>      }
<a name="line147"></a>    } else {
<a name="line148"></a>      if (rows &lt;= 1) {
<a name="line149"></a>        throw Error(goog.ui.Component.Error.ALREADY_RENDERED);
<a name="line150"></a>      }
<a name="line151"></a>      this.userInputEl_.rows = rows;
<a name="line152"></a>    }
<a name="line153"></a>  }
<a name="line154"></a>  this.rows_ = rows;
<a name="line155"></a>};
<a name="line156"></a>
<a name="line157"></a>
<a name="line158"></a>/**
<a name="line159"></a> * @return {number} The number of rows in the user input element.
<a name="line160"></a> */
<a name="line161"></a>goog.ui.Prompt.prototype.getRows = function() {
<a name="line162"></a>  return this.rows_;
<a name="line163"></a>};
<a name="line164"></a>
<a name="line165"></a>
<a name="line166"></a>/**
<a name="line167"></a> * Set the number of cols in the user input element.
<a name="line168"></a> * @param {number} cols Number of cols for user input element.
<a name="line169"></a> */
<a name="line170"></a>goog.ui.Prompt.prototype.setCols = function(cols) {
<a name="line171"></a>  this.cols_ = cols;
<a name="line172"></a>  if (this.userInputEl_) {
<a name="line173"></a>    if (this.userInputEl_.tagName.toLowerCase() == &#39;input&#39;) {
<a name="line174"></a>      this.userInputEl_.size = cols;
<a name="line175"></a>    } else {
<a name="line176"></a>      this.userInputEl_.cols = cols;
<a name="line177"></a>    }
<a name="line178"></a>  }
<a name="line179"></a>};
<a name="line180"></a>
<a name="line181"></a>
<a name="line182"></a>/**
<a name="line183"></a> * @return {number} The number of cols in the user input element.
<a name="line184"></a> */
<a name="line185"></a>goog.ui.Prompt.prototype.getCols = function() {
<a name="line186"></a>  return this.cols_;
<a name="line187"></a>};
<a name="line188"></a>
<a name="line189"></a>
<a name="line190"></a>/**
<a name="line191"></a> * Create the initial DOM representation for the prompt.
<a name="line192"></a> */
<a name="line193"></a>goog.ui.Prompt.prototype.createDom = function() {
<a name="line194"></a>  goog.ui.Prompt.superClass_.createDom.call(this);
<a name="line195"></a>
<a name="line196"></a>  var cls = this.getClass();
<a name="line197"></a>
<a name="line198"></a>  // add input box to the content
<a name="line199"></a>  var attrs = {
<a name="line200"></a>      &#39;className&#39;: cls + &#39;-userInput&#39;,
<a name="line201"></a>      &#39;value&#39;: this.defaultValue_};
<a name="line202"></a>  if (this.rows_ == 1) {
<a name="line203"></a>    // If rows == 1 then use an input element.
<a name="line204"></a>    this.userInputEl_ = /** @type {HTMLInputElement} */
<a name="line205"></a>      (this.getDomHelper().createDom(&#39;input&#39;, attrs));
<a name="line206"></a>    this.userInputEl_.type = &#39;text&#39;;
<a name="line207"></a>    if (this.cols_) {
<a name="line208"></a>      this.userInputEl_.size = this.cols_;
<a name="line209"></a>    }
<a name="line210"></a>  } else {
<a name="line211"></a>    // If rows &gt; 1 then use a textarea.
<a name="line212"></a>    this.userInputEl_ = /** @type {HTMLInputElement} */
<a name="line213"></a>        (this.getDomHelper().createDom(&#39;textarea&#39;, attrs));
<a name="line214"></a>    this.userInputEl_.rows = this.rows_;
<a name="line215"></a>    if (this.cols_) {
<a name="line216"></a>      this.userInputEl_.cols = this.cols_;
<a name="line217"></a>    }
<a name="line218"></a>  }
<a name="line219"></a>
<a name="line220"></a>  var contentEl = this.getContentElement();
<a name="line221"></a>  contentEl.appendChild(this.getDomHelper().createDom(
<a name="line222"></a>      &#39;div&#39;, {&#39;style&#39;: &#39;overflow: auto&#39;}, this.userInputEl_));
<a name="line223"></a>
<a name="line224"></a>  if (this.rows_ &gt; 1) {
<a name="line225"></a>    // Set default button to null so &lt;enter&gt; will work properly in the textarea
<a name="line226"></a>    this.getButtonSet().setDefault(null);
<a name="line227"></a>  }
<a name="line228"></a>};
<a name="line229"></a>
<a name="line230"></a>
<a name="line231"></a>/**
<a name="line232"></a> * Causes the prompt to appear, centered on the screen, gives focus
<a name="line233"></a> * to the text box, and selects the text
<a name="line234"></a> * @param {boolean} visible Whether the dialog should be visible.
<a name="line235"></a> */
<a name="line236"></a>goog.ui.Prompt.prototype.setVisible = function(visible) {
<a name="line237"></a>  goog.ui.Dialog.prototype.setVisible.call(this, visible);
<a name="line238"></a>
<a name="line239"></a>  if (visible) {
<a name="line240"></a>    this.isClosing_ = false;
<a name="line241"></a>    this.userInputEl_.value = this.defaultValue_;
<a name="line242"></a>    if (goog.userAgent.OPERA) {
<a name="line243"></a>      // select() doesn&#39;t focus &lt;input&gt; elements in Opera.
<a name="line244"></a>      this.userInputEl_.focus();
<a name="line245"></a>    }
<a name="line246"></a>    this.userInputEl_.select();
<a name="line247"></a>  }
<a name="line248"></a>};
<a name="line249"></a>
<a name="line250"></a>
<a name="line251"></a>/**
<a name="line252"></a> * Sets the default value of the prompt when it is displayed.
<a name="line253"></a> * @param {string} defaultValue The default value to display.
<a name="line254"></a> */
<a name="line255"></a>goog.ui.Prompt.prototype.setDefaultValue = function(defaultValue) {
<a name="line256"></a>  this.defaultValue_ = defaultValue;
<a name="line257"></a>};
<a name="line258"></a>
<a name="line259"></a>
<a name="line260"></a>/**
<a name="line261"></a> * Handles the closing of the prompt, invoking the callback function that was
<a name="line262"></a> * registered to handle the value returned by the prompt.
<a name="line263"></a> * @param {goog.ui.Dialog.Event} e The dialog&#39;s selection event.
<a name="line264"></a> * @private
<a name="line265"></a> */
<a name="line266"></a>goog.ui.Prompt.prototype.onPromptExit_ = function(e) {
<a name="line267"></a>  /*
<a name="line268"></a>   * The timeouts below are required for one edge case. If after the dialog
<a name="line269"></a>   * hides, suppose validation of the input fails which displays an alert. If
<a name="line270"></a>   * the user pressed the Enter key to dismiss the alert that was displayed it
<a name="line271"></a>   * can trigger the event handler a second time. This timeout ensures that the
<a name="line272"></a>   * alert is displayed only after the prompt is able to clean itself up.
<a name="line273"></a>   */
<a name="line274"></a>  if (!this.isClosing_) {
<a name="line275"></a>    this.isClosing_ = true;
<a name="line276"></a>    if (e.key == &#39;ok&#39;) {
<a name="line277"></a>      goog.Timer.callOnce(
<a name="line278"></a>          goog.bind(this.callback_, this, this.userInputEl_.value), 1);
<a name="line279"></a>    } else {
<a name="line280"></a>      goog.Timer.callOnce(goog.bind(this.callback_, this, null), 1);
<a name="line281"></a>    }
<a name="line282"></a>  }
<a name="line283"></a>};
<a name="line284"></a>
<a name="line285"></a>
<a name="line286"></a>/** @inheritDoc */
<a name="line287"></a>goog.ui.Prompt.prototype.disposeInternal = function() {
<a name="line288"></a>  goog.dom.removeNode(this.userInputEl_);
<a name="line289"></a>
<a name="line290"></a>  goog.events.unlisten(this, goog.ui.Dialog.EventType.SELECT,
<a name="line291"></a>      this.onPromptExit_, true, this);
<a name="line292"></a>
<a name="line293"></a>  goog.ui.Prompt.superClass_.disposeInternal.call(this);
<a name="line294"></a>
<a name="line295"></a>  this.defaulValue_ = null;
<a name="line296"></a>  this.userInputEl_ = null;
<a name="line297"></a>};
</pre>


</body>
</html>
